package com.kintreda.ieco_server.module.article.web;

import com.kintreda.ieco_server.bean.article.Article;
import com.kintreda.ieco_server.core.repository.IBaseRepository;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.article.service.IArticleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedList;
import java.util.List;

/**
 * @Created by IntelliJ IDEA
 * @Project_Name ieco_server
 * @Package com.kintreda.ieco_server.module.api.web
 * @Author ZhongYao_H
 * @Date 2019/5/14:10:06
 * @Year 2019 Month 05 Day 14
 * @Date_zh 2019 年 五月 星期二
 */
@Controller
@RequestMapping("/article")
public class ArticleController {


    @Autowired
    IBaseRepository<Article,String> articleStringIBaseRepository;
    @Autowired
    IArticleService articleService;

    @RequestMapping("/add")
    public ModelAndView edit(){
        return new ModelAndView("article/article-add");
    }


    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Object add(Article article){
        try {

            if (StringUtils.equals("2",article.getType())){
                //如果发布的是分类攻略则删除旧的分类攻略  分类攻略只有一条
                articleService.deleteByPK("from Article where type = ?",new String[]{"2"});
            }
            articleService.save(article);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @RequestMapping("/list")
    public ModelAndView articleList(){
        return new ModelAndView("article/article-list");
    }


    @RequestMapping(value = "/query",method = RequestMethod.POST)
    @ResponseBody
    public Object query(String title){

        StringBuffer sql = new StringBuffer("select id,title,anno,type,createDate from arti ");
        List<Object> params = new LinkedList<>();
        if (StringUtils.isNotBlank(title)){
            params = new LinkedList<>();
            sql.append(" where title like ?  ");
            params.add(title);
        }

        sql.append(" order by createDate desc ");
        return articleStringIBaseRepository.findListBySql(sql.toString(),params.toArray());

    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Object delete(String id){
        try {
            articleService.deleteByPK(id);
            return R.ok("删除成功!");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("异常! 稍后重试!");
        }
    }
    @RequestMapping(value = "/details")
    public ModelAndView details(String id){
        ModelAndView andView =  new ModelAndView("article/details");
        Article article = articleService.get(id);
        andView.addObject("article",article);
        return andView;
    }


    @RequestMapping("/modfiy")
    public ModelAndView modfiy(String id){
        ModelAndView modelAndView = new ModelAndView("article/article-edit");
        Article article = articleService.get(id);
        modelAndView.addObject("article",article);
        return modelAndView;

    }


    @RequestMapping(value = "/modfiy",method = RequestMethod.POST)
    @ResponseBody
    public Object modfiy(Article article){
        Article oldArticle = articleService.get(article.getId());
        if (oldArticle==null){
            return R.error("修改文章不存在!");
        }

        oldArticle.setAnno(article.getAnno());
        oldArticle.setBody(article.getBody());
        oldArticle.setCoverId(article.getCoverId());
        oldArticle.setType(article.getType());
        oldArticle.setTitle(article.getTitle());
        articleService.update(oldArticle);
        return R.ok("修改成功");
    }



}
